package cn.bellychang.leetcode.question203;

/**
 * @author ChangLiang
 * @date 2020/9/9
 */


/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
public class Solution {
    public ListNode removeElements(ListNode head, int val) {

        ListNode prevNode = null;
        ListNode temp = head;

        while (temp != null) {
            // 判断是否是head
            if (temp==head && temp.val == val) {
                head = head.next;
                temp = head;
            } else {
                if (temp.val == val) {
                    prevNode.next = temp.next;
                } else {
                    prevNode = temp;
                }
                temp = temp.next;
            }
        }
        return head;
    }
}
